﻿unit CmRegionService;

interface
{$I ../Config.inc}
uses
  FastMM4,
  FastMove,
  System.SysUtils,
  System.Classes,
  System.Types,
  Winapi.Windows,
  Data.DB,
  Data.Win.ADODB,
  Datasnap.DBClient,
  Datasnap.Provider,
  {$IFDEF UNICODE}
  AnsiStrings,
  {$ENDIF }
  QBParcel,
  DllSpread,
  Generics.Collections,
  DateUtils,
  QJson,
  UserConnection,
  DBAccessor,
  DllPlugin,
  StrUtils,
  SK0001Comm,
  CmRegionModel,
  CmRegionBLL,
  CmCustomerFileModel;

type

  TCmRegionService = class(TCmRegionBLL)
  private

  protected

  public
  {$IFDEF SERVER}
    function BeforeTreatment(const iItem:TCmRegion; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeTreatment(const iItems:TCmRegionList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function BeforeCheck(const iItem:TCmRegion; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeCheck(const iItems:TCmRegionList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function Check(const iItem:TCmRegion; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function Check(const iItems:TCmRegionList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function BeforeExecute(const iItem:TCmRegion; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function BeforeExecute(const iItems:TCmRegionList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function AfterTreatment(const iItem:TCmRegion; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;
    function AfterTreatment(const iItems:TCmRegionList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;overload;override;

    function CalculateIdAndSortNo(const iPid:string; out oNewSortNo:Integer; out oNewId,oError:string):Boolean;
  {$ENDIF}
    function LoadAll(const iOrgId:string; const iItems:TCmRegionList; out oError:string; const iJustUse:Boolean=False):Boolean;
    function ExchangeSortNo(const iItem1:TCmRegion; const iItem2:TCmRegion; out oError:string):Boolean;
    function Use(const iItem:TCmRegion; out oError:string):Boolean;
  end;
  
implementation

{$IFDEF SERVER}
function TCmRegionService.BeforeTreatment(const iItem:TCmRegion; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
var
  ItemDB:TCmRegion;
  NewSortNo:Integer;
  NewId:string;
begin
  if iItem<>nil then
  begin
    ItemDB:=TCmRegion.Create;
    try
      case iPreMode of
      pmAdd:
        begin
          if not CalculateIdAndSortNo(iItem.RegionTreePid,NewSortNo,NewId,oError) then
          begin
            oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
          iItem.RegionId:=iItem.ObjGUID;
          iItem.RegionTreeId:=NewId;
          iItem.RegionIsSystem:=False;
          iItem.RegionStatus:=1;
          iItem.RegionSortNo:=NewSortNo;
          iItem.RecordDate:=Now;

          iItem.TimeStamp:=TSK0001Comm.GetTimeStampMs;
        end;
      pmEdit:
        begin
          if not iParcel.GoodsExists(AnsiString(iItem.ClassName)) then
          begin
            if ItemDB.LoadFromStr(iItem.SaveToStr,oError) and
              Reload(ItemDB,oError) then
            begin
              iParcel.PutStringGoods(AnsiString(iItem.ClassName),ItemDB.SaveToStr);
            end else begin
              oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
              Result:=False;
              Exit;
            end;
          end else if not ItemDB.LoadFromStr(iParcel.GetStringGoods(AnsiString(iItem.ClassName)),oError) then
          begin
            oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;

          iItem.ModifyDate:=Now;
        end;
      pmDelete:
        begin
          if not Reload(iItem,oError) then
          begin
            oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
        end;
      end;
    finally
      FreeAndNil(ItemDB);
    end;
  end else begin
    oError:=Format(string('调用%s.BeforeTreatment错误...错误内容:传入的对象或字符串为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmRegionService.BeforeTreatment(const iItems:TCmRegionList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
var
  Key:string;
  ItemsDB:TCmRegionList;
begin
  if iItems<>nil then
  begin
    ItemsDB:=TCmRegionList.Create;
    try
      case iPreMode of
      pmAdd:
        begin
        end;
      pmEdit:
        begin
          if not iParcel.GoodsExists(AnsiString(iItems.ClassName)) then
          begin
            if ItemsDB.LoadFromStr(iItems.SaveToStr,oError) and
              Reload(ItemsDB,oError) then
            begin
              iParcel.PutStringGoods(AnsiString(iItems.ClassName),ItemsDB.SaveToStr);
            end else begin
              oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
              Result:=False;
              Exit;
            end;
          end else if not ItemsDB.LoadFromStr(iParcel.GetStringGoods(AnsiString(iItems.ClassName)),oError) then
          begin
            oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
        end;
      pmDelete:
        begin
          if not Reload(iItems,oError) then
          begin
            oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
        end;
      end;
      for Key in iItems.SortKeys do
      begin
        if iPreMode=pmEdit then
        begin
          iParcel.PutStringGoods(AnsiString(iItems.Items[Key].ClassName),ItemsDB.Items[Key].SaveToStr);
        end;
        if not BeforeTreatment(iItems.Items[Key],iPreMode,iParcel,oError) then
        begin
          oError:=Format(string('调用%s.BeforeTreatment错误 --> %s'),[Self.ClassName,oError]);
          Result:=False;
          Exit;
        end;
      end;
    finally
      FreeAndNil(ItemsDB);
    end;
  end else begin
    oError:=Format(string('调用%s.BeforeTreatment错误...错误内容:传入的对象或字符串为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmRegionService.BeforeCheck(const iItem:TCmRegion; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TCmRegionService.BeforeCheck(const iItems:TCmRegionList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TCmRegionService.Check(const iItem:TCmRegion; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
var
  ItemDB:TCmRegion;
  SQL,SQLResultStr:string;
begin
  if iItem<>nil then
  begin
    ItemDB:=TCmRegion.Create;
    try
      if SameStr(Trim(iItem.RegionId),EmptyStr) then
      begin
        oError:='地域ID不允许为空';
        Result:=False;
        Exit;
      end else if SameStr(Trim(iItem.RegionTreeId),EmptyStr) then
      begin
        oError:='地域树ID不允许为空';
        Result:=False;
        Exit;
      end else if SameStr(Trim(iItem.RegionTreePid),EmptyStr) then
      begin
        oError:='地域树PID不允许为空';
        Result:=False;
        Exit;
      end else if SameStr(Trim(iItem.RegionName),EmptyStr) then
      begin
        oError:='地域名称不允许为空';
        Result:=False;
        Exit;
      end else if iItem.RegionIsSystem then
      begin
        oError:='不允许创建、修改、删除系统级地域';
        Result:=False;
        Exit;
      end else if (iItem.RegionStatus<0) or (iItem.RegionStatus>1) then
      begin
        oError:='地域状态非法';
        Result:=False;
        Exit;
      end else if SameStr(Trim(iItem.OrgId),EmptyStr) then
      begin
        oError:='归属组织不允许为空';
        Result:=False;
        Exit;
      end;

      case iPreMode of
      pmAdd:
        begin
          if SameStr(Trim(iItem.RecordPsn),EmptyStr) then
          begin
            oError:='录入人不允许为空';
            Result:=False;
            Exit;
          end;
        end;
      pmEdit:
        begin
          if not iParcel.GoodsExists(AnsiString(iItem.ClassName)) then
          begin
            if ItemDB.LoadFromStr(iItem.SaveToStr,oError) and
              Reload(ItemDB,oError) then
            begin
              iParcel.PutStringGoods(AnsiString(iItem.ClassName),ItemDB.SaveToStr);
            end else begin
              oError:=Format(string('调用%s.Check错误 --> %s'),[Self.ClassName,oError]);
              Result:=False;
              Exit;
            end;
          end else if not ItemDB.LoadFromStr(iParcel.GetStringGoods(AnsiString(iItem.ClassName)),oError) then
          begin
            oError:=Format(string('调用%s.Check错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
          if SameStr(Trim(iItem.ModifyPsn),EmptyStr) then
          begin
            oError:='地域人不允许为空';
            Result:=False;
            Exit;
          end else if iItem.RegionId<>ItemDB.RegionId then
          begin
            oError:='地域ID不允许修改';
            Result:=False;
            Exit;
          end else if iItem.RegionTreeId<>ItemDB.RegionTreeId then
          begin
            oError:='地域树ID不允许修改';
            Result:=False;
            Exit;
          end else if iItem.RegionTreePid<>ItemDB.RegionTreePid then
          begin
            oError:='地域树PID不允许修改';
            Result:=False;
            Exit;
          end else if iItem.RegionIsSystem<>ItemDB.RegionIsSystem then
          begin
            oError:='地域类型不允许修改';
            Result:=False;
            Exit;
          end else if not SameStr(Trim(iItem.RecordPsn),Trim(ItemDB.RecordPsn)) then
          begin
            oError:='录入人不允许修改';
            Result:=False;
            Exit;
          end else if not SameStr(Trim(iItem.OrgId),Trim(ItemDB.OrgId)) then
          begin
            oError:='归属组织不允许修改';
            Result:=False;
            Exit;
          end else if not SameDateTime(iItem.RecordDate,ItemDB.RecordDate) then
          begin
            oError:='录入日期不允许修改';
            Result:=False;
            Exit;
          end else if iItem.TimeStamp<>ItemDB.TimeStamp then
          begin
            oError:='地域信息已被他人修改，请重新装载...';
            Result:=False;
            Exit;
          end;
        end;
      pmDelete:
        begin
          SQL:=Format(string('SELECT COUNT(%s) FROM %s WHERE %s=''%s'' '),
                    [TCmCustomerFileModel.cRegionIdKey,TCmCustomerFileModel.cTblName,
                    TCmCustomerFileModel.cRegionIdKey,iItem.RegionId]);
          if TSK0001Comm.ReadSimpleData(fDBConn,fDBID,SQL,SQLResultStr,oError) then
          begin
            if StrToInt(SQLResultStr)>0 then
            begin
              oError:=Format(string('调用%s.Check错误...错误内容:当前地域已绑定客户,请先解除客户关系...'),[Self.ClassName]);
              Result:=False;
              Exit;
            end;
          end else begin
            oError:=Format(string('调用%s.Check错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;

          if iItem.RegionStatus<>0 then
          begin
            oError:='只允许删除已冻结地域';
            Result:=False;
            Exit;
          end
        end;
      end;
    finally
      FreeAndNil(ItemDB);
    end;
  end else begin
    oError:=Format(string('调用%s.Check错误...错误内容:传入的对象或字符串为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmRegionService.Check(const iItems:TCmRegionList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
var
  Key:string;
  ItemsDB:TCmRegionList;
begin
  if iItems<>nil then
  begin
    ItemsDB:=TCmRegionList.Create;
    try
      case iPreMode of
      pmAdd:
        begin
        end;
      pmEdit:
        begin
          if not iParcel.GoodsExists(AnsiString(iItems.ClassName)) then
          begin
            if ItemsDB.LoadFromStr(iItems.SaveToStr,oError) and
              Reload(ItemsDB,oError) then
            begin
              iParcel.PutStringGoods(AnsiString(iItems.ClassName),ItemsDB.SaveToStr);
            end else begin
              oError:=Format(string('调用%s.Check错误 --> %s'),[Self.ClassName,oError]);
              Result:=False;
              Exit;
            end;
          end else if not ItemsDB.LoadFromStr(iParcel.GetStringGoods(AnsiString(iItems.ClassName)),oError) then
          begin
            oError:=Format(string('调用%s.Check错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
        end;
      pmDelete:
        begin
        end;
      end;
      for Key in iItems.SortKeys do
      begin
        if iPreMode=pmEdit then
        begin
          iParcel.PutStringGoods(AnsiString(iItems.Items[Key].ClassName),ItemsDB.Items[Key].SaveToStr);
        end;
        if not Check(iItems.Items[Key],iPreMode,iParcel,oError) then
        begin
          oError:=Format(string('调用%s.Check错误 --> %s'),[Self.ClassName,oError]);
          Result:=False;
          Exit;
        end;
      end;
    finally
      FreeAndNil(ItemsDB);
    end;
  end else begin
    oError:=Format(string('调用%s.Check错误...错误内容:传入的对象或字符串为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmRegionService.BeforeExecute(const iItem:TCmRegion; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  if iItem<>nil then
  begin
    case iPreMode of
    pmAdd:
      begin
        iItem.TimeStamp:=TSK0001Comm.GetTimeStampMs;
      end;
    pmEdit:
      begin
        iItem.TimeStamp:=TSK0001Comm.GetTimeStampMs;
      end;
    pmDelete:
      begin
      end;
    pmUse:
      begin
      end;
    pmUnUse:
      begin
      end;
    end;
  end else begin
    oError:=Format(string('调用%s.BeforeExecute错误...错误内容:传入的对象或字符串为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmRegionService.BeforeExecute(const iItems:TCmRegionList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
var
  Key:string;
  ItemsDB:TCmRegionList;
begin
  if iItems<>nil then
  begin
    ItemsDB:=TCmRegionList.Create;
    try
      case iPreMode of
      pmAdd:
        begin
        end;
      pmEdit:
        begin
          if not iParcel.GoodsExists(AnsiString(iItems.ClassName)) then
          begin
            if ItemsDB.LoadFromStr(iItems.SaveToStr,oError) and
              Reload(ItemsDB,oError) then
            begin
              iParcel.PutStringGoods(AnsiString(iItems.ClassName),ItemsDB.SaveToStr);
            end else begin
              oError:=Format(string('调用%s.BeforeExecute错误 --> %s'),[Self.ClassName,oError]);
              Result:=False;
              Exit;
            end;
          end else if not ItemsDB.LoadFromStr(iParcel.GetStringGoods(AnsiString(iItems.ClassName)),oError) then
          begin
            oError:=Format(string('调用%s.BeforeExecute错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
        end;
      pmDelete:
        begin
        end;
      end;
      for Key in iItems.SortKeys do
      begin
        if iPreMode=pmEdit then
        begin
          iParcel.PutStringGoods(AnsiString(iItems.Items[Key].ClassName),ItemsDB.Items[Key].SaveToStr);
        end;
        if not BeforeCheck(iItems.Items[Key],iPreMode,iParcel,oError) then
        begin
          oError:=Format(string('调用%s.BeforeExecute错误 --> %s'),[Self.ClassName,oError]);
          Result:=False;
          Exit;
        end;
      end;
    finally
      FreeAndNil(ItemsDB);
    end;
  end else begin
    oError:=Format(string('调用%s.BeforeExecute错误...错误内容:传入的对象或字符串为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmRegionService.AfterTreatment(const iItem:TCmRegion; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TCmRegionService.AfterTreatment(const iItems:TCmRegionList; const iPreMode:ePretreatmentMode; const iParcel:TQBParcel; out oError:string):Boolean;
begin
  Result:=True;
end;

function TCmRegionService.CalculateIdAndSortNo(const iPid:string; out oNewSortNo:Integer; out oNewId,oError:string):Boolean;
var
  SQL,MaxId,NumStr,MaxSortNoStr:string;
  NewNum:Integer;
begin
  if SameStr(Trim(iPid),EmptyStr) then
  begin
    oError:=Format(string('调用%s.CalculateIdAndSortNo方法失败, 错误内容:传入的iPid为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;

  SQL:=Format(string('SELECT MAX(%s) AS %s FROM %s WHERE %s=''%s'';'),
          [TCmRegionModel.cRegionTreeIdKey,TCmRegionModel.cRegionTreeIdKey,
          TCmRegionModel.cTblName,TCmRegionModel.cRegionTreePidKey,
          iPid]);
  if TSK0001Comm.ReadSimpleData(fDBConn,EmptyStr,SQL,MaxId,oError) then
  begin
    if SameStr(Trim(MaxId),EmptyStr) then
    begin
      oNewId :=iPid+'.001';
    end else begin
      NumStr:=string(TSK0001Comm.AfterStr(AnsiString(MaxId),AnsiString(iPid+'.')));
      if Length(NumStr)=3 then
      begin
        NewNum:=StrToInt(NumStr[1])*100+StrToInt(NumStr[2])*10+StrToInt(NumStr[3])+1;
        if NewNum<10 then
        begin
          oNewId :=iPid+'.00'+IntToStr(NewNum);
        end else if NewNum<100 then
        begin
          oNewId :=iPid+'.0'+IntToStr(NewNum);
        end else if NewNum<1000 then
        begin
          oNewId :=iPid+'.'+IntToStr(NewNum);
        end else begin
          oError:=Format(string('调用%s.CalculateIdAndSortNo方法失败,错误内容:新Id=1000 越界溢出!'),[Self.ClassName]);
          Result:=False;
          Exit;
        end;
      end else begin
        oError:=Format(string('调用%s.CalculateIdAndSortNo方法失败,错误内容:截取的id尾数字符串长度不等于3!'),[Self.ClassName]);
        Result:=False;
        Exit;
      end;
    end;
  end else begin
    oError:=Format(string('调用%s.CalculateIdAndSortNo方法失败,错误内容:获取最大同级Id失败!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;

  SQL:=Format(string('SELECT MAX(%s) AS %s FROM %s WHERE %s=''%s'';'),
          [TCmRegionModel.cRegionSortNoKey,TCmRegionModel.cRegionSortNoKey,
          TCmRegionModel.cTblName,TCmRegionModel.cRegionTreePidKey,
          iPid]);
  if TSK0001Comm.ReadSimpleData(fDBConn,EmptyStr,SQL,MaxSortNoStr,oError) then
  begin
    if SameStr(Trim(MaxSortNoStr),EmptyStr) then
    begin
      oNewSortNo:=1;
    end else begin
      oNewSortNo:=StrToInt(MaxSortNoStr)+1;
    end;
  end else begin
    oError:=Format(string('调用%s.CalculateIdAndSortNo方法失败, 错误内容:获取最大同级SortNo失败!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;
{$ENDIF}

function TCmRegionService.LoadAll(const iOrgId:string; const iItems:TCmRegionList; out oError:string; const iJustUse:Boolean=False):Boolean;
var
  Condition,OrderBy:string;
begin
  if (not SameStr(iOrgId,EmptyStr)) and (iItems<>nil) then
  begin
    if iJustUse then
    begin
      Condition:=Format(string('%s=''%s'' and %s=1'),[TCmRegionModel.cOrgIdKey,iOrgId,TCmRegionModel.cRegionStatusKey]);
    end else begin
      Condition:=Format(string('%s=''%s'''),[TCmRegionModel.cOrgIdKey,iOrgId]);
    end;
    OrderBy:=TCmRegionModel.cRegionTreePidKey+','+TCmRegionModel.cRegionSortNoKey+' ASC';

    if not LoadByCondition(iItems,Condition,oError,OrderBy) then
    begin
      Result:=False;
      Exit;
    end;
  end else begin
    oError:=Format(string('调用%s.LoadAll方法失败！错误内容:传入的iOrgId或iItems为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmRegionService.ExchangeSortNo(const iItem1:TCmRegion; const iItem2:TCmRegion; out oError:string):Boolean;
var
  TempSortNo:Integer;
  List:TCmRegionList;
  NewItem1,NewItem2:TCmRegion;
begin
  if (iItem1<>nil) and
    (iItem2<>nil) then
  begin
    List:=TCmRegionList.Create;
    NewItem1:=TCmRegion.Create;
    NewItem2:=TCmRegion.Create;
    try
      if NewItem1.LoadFromStr(iItem1.SaveToStr,oError) and
        NewItem2.LoadFromStr(iItem2.SaveToStr,oError) then
      begin
        TempSortNo:=iItem1.RegionSortNo;
        NewItem1.RegionSortNo:=iItem2.RegionSortNo;
        NewItem2.RegionSortNo:=TempSortNo;

        List.Add(NewItem1);
        List.Add(NewItem2);
      end else begin
        oError:=Format(string('调用%s.ExchangeSortNo方法失败！错误内容%s:'),[Self.ClassName,oError]);
        Result:=False;
        Exit;
      end;
      if Inherited Edit(List,oError) then
      begin
        if iItem1.LoadFromStr(NewItem1.SaveToStr,oError) and
          iItem2.LoadFromStr(NewItem2.SaveToStr,oError) then
        begin

        end else begin
          oError:=Format(string('调用%s.ExchangeSortNo方法失败！错误内容%s:'),[Self.ClassName,oError]);
          Result:=False;
          Exit;
        end;
      end else begin
        oError:=Format(string('调用%s.ExchangeSortNo方法失败！错误内容%s:'),[Self.ClassName,oError]);
        Result:=False;
        Exit;
      end;
    finally
      List.Free;
    end;
  end else begin
    oError:=Format(string('调用%s.ExchangeSortNo方法失败！错误内容:传入的iItem1或者iItem2为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;

function TCmRegionService.Use(const iItem:TCmRegion; out oError:string):Boolean;
var
  Condition,SQL,SimpleResult,Key:string;
  List:TCmRegionList;
begin
  if (iItem<>nil) then
  begin
    if iItem.RegionStatus=1 then
    begin
      Condition:=Format(string('%s LIKE ''%s'' '),[TCmRegionModel.cRegionTreeIdKey,iItem.RegionTreeId+'%']);
      List:=TCmRegionList.Create;
      try
        if not LoadByCondition(List,Condition,oError) then
        begin
          oError:=Format(string('调用%s.Use方法失败！错误内容:%s'),[Self.ClassName,oError]);
          Result:=False;
          Exit;
        end;
        for Key in List.SortKeys do
        begin
          List.Items[Key].RegionStatus:=0;
          List.Items[Key].ModifyPsn:=iItem.ModifyPsn;
        end;
        if not inherited Edit(List,oError) then
        begin
          oError:=Format(string('调用%s.Use方法失败！错误内容:%s'),[Self.ClassName,oError]);
          Result:=False;
          Exit;
        end;
      finally
        List.Free;
      end;
    end else begin
      SQL:=Format(string('SELECT %s FROM %s WHERE %s=''%s'' AND %s=%d'),
                  [TCmRegionModel.cRegionIdKey,TCmRegionModel.cTblName,
                  TCmRegionModel.cRegionTreeIdKey,iItem.RegionTreePid,
                  TCmRegionModel.cRegionStatusKey,1]);
      if TSK0001Comm.ReadSimpleData(fDBConn,fDBID,SQL,SimpleResult,oError) then
      begin
        if SameStr(Trim(SimpleResult),EmptyStr) then
        begin
          oError:=Format(string('上级地域已停用,请先启用上级地域'),[Self.ClassName,oError]);
          Result:=False;
          Exit;
        end else begin
          iItem.RegionStatus:=1;
          if not inherited Edit(iItem,oError) then
          begin
            oError:=Format(string('调用%s.Use错误 --> %s'),[Self.ClassName,oError]);
            Result:=False;
            Exit;
          end;
        end;
      end else begin
        oError:=Format(string('调用%s.Use错误 --> %s'),[Self.ClassName,oError]);
        Result:=False;
        Exit;
      end;
    end;
  end else begin
    oError:=Format(string('调用%s.Use方法失败！错误内容:传入的iItem为空!'),[Self.ClassName]);
    Result:=False;
    Exit;
  end;
  Result:=True;
end;
  
end.
